package com.sinosoft.generateDataInfo.common.xmlService;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

public abstract class XmlProducerDom4j {

	private static Document doc;
	private static List<XmlInfoBean> infos;
	
	public static String generateXMLString(List<XmlInfoBean> xmlInfoBeans) {
		infos = xmlInfoBeans;
		initXMLFileDocumentContext();
		buildDoc();
		return  getStringFromDocumentXml();
	}

	private static void initXMLFileDocumentContext(){
		doc = DocumentHelper.createDocument();
	}
	
	private static void buildDoc(){
		if(infos == null) return;
		XmlInfoBean rootInfo = null;
		for(int i=0;i<infos.size();i++){
			XmlInfoBean info = infos.get(i);
			if((info.getLevel()+"").equals("0")){
				rootInfo = info;
				infos.remove(i);
				break;
			}
		}
		if(rootInfo != null){
			String name = rootInfo.getName();
			String content = rootInfo.getContent();
			Map<String,String> propertiesMap = rootInfo.getProperties();
			Element root = doc.addElement(name);
			copyText(root,content);
			copyProperties(root,propertiesMap);
			build(root,rootInfo);
		}
	}
	
	private static void copyText(Element e , String content){
		if(content!=null ){
			e.setText(content);
		}
	}
	
	private static void copyProperties(Element e , Map<String,String> propertiesMap){
		if(propertiesMap != null && propertiesMap.size()>0){
			Iterator<String> proNames = propertiesMap.keySet().iterator();
			while(proNames.hasNext()){
				String pname = proNames.next();
				e.addAttribute(pname, propertiesMap.get(pname)+"");
			}
		}
	}
	
	private static void build(Element e ,XmlInfoBean parentXmlInfoBean){
		for(int i=0;i<infos.size();i++){
			XmlInfoBean info = infos.get(i);
			if(info.getParentId().equals(parentXmlInfoBean.getMyid())){
				Element mye = e.addElement(info.getName());
				copyText(mye,info.getContent());
				copyProperties(mye,info.getProperties());
				build(mye,info);
			}
		}
	}
	
	private static String getStringFromDocumentXml(){
		return doc.asXML();
	}
	
	/*public static void main(String []args){
		IXmlProducer i = new XmlProducerDom4jImpl();
		XmlInfoBean x1 = new XmlInfoBean();
		XmlInfoBean x11 = new XmlInfoBean();
		XmlInfoBean x12 = new XmlInfoBean();
		XmlInfoBean x111 = new XmlInfoBean();
		XmlInfoBean x121 = new XmlInfoBean();
		XmlInfoBean x1111 = new XmlInfoBean();
		
		x1.setName("我是老大");
		x1.setLevel(0);
		x1.setMyid("x1");
		Map m = new HashMap();
		m.put("f123123", "123123");
		m.put("ff", "1af23123");
		m.put("ww", "1231fd23");
		x1.setProperties(m);
		
		x11.setName("我是11");
		x11.setContent("11");
		x11.setMyid("x11");
		x11.setParentId("x1");
		
		x12.setName("我是12");
		x12.setContent("12");
		x12.setMyid("x12");
		x12.setParentId("x1");
		
		x111.setName("我是111");
		x111.setContent("111");
		x111.setMyid("x111");
		x111.setParentId("x11");
		
		x121.setName("我是121");
		x121.setMyid("x121");
		x121.setParentId("x12");
		
		x1111.setName("我是1111");
		x1111.setContent("1111");
		x1111.setMyid("x1111");
		x1111.setParentId("x111");
		Map m2 = new HashMap();
		m2.put("w123123", "123123");
		m2.put("ff", "1af23123");
		m2.put("ww", "1231fd23");
		x121.setProperties(m2);
		
		
		List<XmlInfoBean> l = new ArrayList<XmlInfoBean>();
		l.add(x1);
		l.add(x11);
		l.add(x12);
		l.add(x111);
		l.add(x121);
		l.add(x1111);
		
		String a  = i.generate(l);
		System.out.println(a);
	}*/
	
}
